<?php
namespace App\Utilitys;

class FFmpeg
{
    /**
     * 使用FFmpeg合成
     */

    protected $ffmpeg; //FFmpeg软件的目录地址
    protected $prefix; //合成后文件地址前缀
    protected $base;
    protected $log;

    public function __construct()
    {
        $this->ffmpeg = 'ffmpeg';
        $this->prefix = '/www/liujiu/quanzi/public/';
        $this->base = new FFbase();
        $this->log = new Logs();
    }

    /**
     * 合成后文件地址前缀
     */
    public function getPrefix()
    {
        $prefix = 'uploads/' . date('Ymd', time()) . '/';
        if (!file_exists($prefix)) {
            mkdir($prefix, 0777);
        }
        return $prefix;
    }

    /**
     * ID==1合成3个视频
     */
    public function setCompTo1($m1,$m2,$m3)
    {
        //ffmpeg \
        //-i /www/liujiu/quanzi/public/uploads/20201125/1606285925739.mp4 \
        //-i /www/liujiu/quanzi/public/uploads/20201125/1606285954228.mp4 \
        //-i /www/liujiu/quanzi/public/uploads/20201125/1606285984231.mp4 \
        //-filter_complex "[0:v]pad=iw:ih*3[a];[a][1:v]overlay=0:h[b];[b][2:v]overlay=0:h*2" \
        //-y /www/liujiu/quanzi/public/uploads/20201125/out002.mp4
        try {
            $prefix = $this->getPrefix();
            $filePath = $prefix.time().rand(100,999).".mp4";
            //拼接指令字符串
            $e = $this->ffmpeg;
            $e .= " -i ".$this->prefix.$m1;
            $e .= " -i ".$this->prefix.$m2;
            $e .= " -i ".$this->prefix.$m3;
            $e .= " -filter_complex \"";
            $e .= "[0:v]pad=iw:ih*3[a];[a][1:v]overlay=0:h[b];[b][2:v]overlay=0:h*2\"";
            $e .= " -y ".$this->prefix.$filePath;
            $this->log->setFFmpeg($e);
            shell_exec($e);
            return array('code'=>'0','msg'=>'success','data'=>$filePath);
        } catch (\Exception $e) {
            return array('code'=>'-1','msg'=>'error');
        }
    }
}